package com.one.blocks.rbac.convert;

import com.one.blocks.mvc.convert.BaseDomainConvert;
import com.one.blocks.rbac.controller.query.SysOrgQuery;
import com.one.blocks.rbac.controller.vo.SysOrgGrantVo;
import com.one.blocks.rbac.controller.vo.SysOrgTreeVo;
import com.one.blocks.rbac.controller.vo.SysOrgVo;
import com.one.blocks.rbac.domain.SysOrg;
import com.one.blocks.rbac.domain.SysUserOrg;
import com.one.blocks.rbac.util.SubjectHelper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2023-10-25 10:58:07
 */
@Mapper(uses = {TimestampConvert.class}, imports = {SubjectHelper.class})
public interface SysOrgConvert extends BaseDomainConvert<SysOrg, SysOrgVo, SysOrgQuery> {

    public static SysOrgConvert INSTANCE = Mappers.getMapper(SysOrgConvert.class);

    List<SysOrgTreeVo> entityToTreeList(List<SysOrg> sysOrgs);

    SysOrgTreeVo entityToTree(SysOrg sysOrg);

    default List<SysUserOrg> grantVoToGrantEntity(SysOrgGrantVo grantVo) {
        if (CollectionUtils.isEmpty(grantVo.getUserIds())) {
            return null;
        }
        List<SysUserOrg> dataToInsert = new ArrayList<>(grantVo.getUserIds().size());
        for (Integer userId : grantVo.getUserIds()) {
            SysUserOrg entity = new SysUserOrg();

            entity.setOrgId(grantVo.getOrgId());
            entity.setUserId(userId);
            entity.setCreateId(SubjectHelper.getSubjectId());
            entity.setCreateTime(SubjectHelper.getAccessTime());
            dataToInsert.add(entity);
        }
        return dataToInsert;
    };

    List<SysOrgTreeVo> entityToTreeVoList(List<SysOrg> allChildrenByFullCode);

//    SysOrg treeVoToEntity(SysOrgTreeVo node);
}